﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XModem.library
{
   public  class XmodemData
    {
        /// <summary>
        /// c数据打包
        /// </summary>
        public static List<List<byte>> xmodem_C = new List<List<byte>>();
        /// <summary>
        /// 1k c数据打包
        /// </summary>
        public static List<List<byte>> xmodem1k_C = new List<List<byte>>();
        /// <summary>
        /// 15数据打包
        /// </summary>
        public static List<List<byte>> xmodem_N = new List<List<byte>>();
        /// <summary>
        /// 1k 15数据打包
        /// </summary>
        public static List<List<byte>> xmodem1k_N = new List<List<byte>>();


        /// <summary>
        /// 打包数据
        /// </summary>
        /// <param name="_path">文件路径</param>
        /// <param name="useBlock1K">true 1k数据 false 数据</param>
        public static void XmodemDataList(string _path,bool useBlock1K) {

            if (useBlock1K)
            {
                xmodem1k_C = new List<List<byte>>();
                xmodem1k_N = new List<List<byte>>();
                xmodem_C = null;
                xmodem_N = null;
            }
            else {
                xmodem_C = new List<List<byte>>();
                xmodem_N = new List<List<byte>>();
                xmodem1k_C = null;
                xmodem1k_N = null;
            }

            CRC crc8 = new CRC8();
            CRC crc16 = new CRC16();
            using (FileStream plik = File.OpenRead(_path))
            {
                using (BinaryReader czytnik = new BinaryReader(plik))
                {

                    List<byte> c_list = new List<byte>();
                    List<byte> n_list = new List<byte>();
                    //包序号
                    byte PackageTemp = 1;

                    while (czytnik.BaseStream.Position != czytnik.BaseStream.Length )
                    {
                        c_list = new List<byte>();
                        n_list = new List<byte>();
                        if (useBlock1K)
                        {
                            c_list.Add(0x02);
                            n_list.Add(0x02);
                        } else {
                            c_list.Add(0x01);
                            n_list.Add(0x01);
                        }
                        c_list.Add(Convert.ToByte((PackageTemp).ToString()));
                        n_list.Add(Convert.ToByte((PackageTemp).ToString()));

                        c_list.Add(Convert.ToByte((255 - PackageTemp).ToString()));
                        n_list.Add(Convert.ToByte((255 - PackageTemp).ToString()));

                        if (useBlock1K)
                        {
                            byte[] byteTemp = new byte[1024];
                            ///初始化byte数组数据
                            for (int z = 0; z < 1024; z++)
                            {
                                byteTemp[z] = 0x1A;
                            }
                            ///读取文件数据进行填充
                            for (int j = 0; j < 1024; j++)
                            {
                                byteTemp[j] = (byte)plik.ReadByte();
                            }
                            foreach (var itemB in byteTemp)
                            {
                                c_list.Add(itemB);
                                n_list.Add(itemB);
                            }
                            var crcbit = BitConverter.GetBytes(crc16.calcCRC(byteTemp));
                            c_list.Add(crcbit[1]);
                            c_list.Add(crcbit[0]);

                            n_list.Add(BitConverter.GetBytes(crc8.calcCRC(byteTemp))[0]);

                            xmodem1k_C.Add(c_list);
                            xmodem1k_N.Add(n_list);
                            PackageTemp++;
                            //if (PackageTemp ==256)
                            //{
                            //    PackageTemp = 0;
                            //}
                        }
                        else {
                            byte[] byteTemp = new byte[128];
                            ///初始化byte数组数据
                            for (int z = 0; z < 128; z++)
                            {
                                byteTemp[z] = 0x1A;
                            }
                            ///读取文件数据进行填充
                            for (int j = 0; j < 128; j++)
                            {
                                byteTemp[j] = (byte)plik.ReadByte();
                            }
                            foreach (var itemB in byteTemp)
                            {
                                c_list.Add(itemB);
                                n_list.Add(itemB);
                            }
                            var crcbit = BitConverter.GetBytes(crc16.calcCRC(byteTemp));
                            c_list.Add(crcbit[1]);
                            c_list.Add(crcbit[0]);

                            n_list.Add(BitConverter.GetBytes(crc8.calcCRC(byteTemp))[0]);

                            xmodem_C.Add(c_list);
                            xmodem_N.Add(n_list);
                            PackageTemp++;
                            //if (PackageTemp == 255)
                            //{
                            //    PackageTemp = 0;
                            //}
                        }

                       


                    }

                }
            }

        }
        
    }


    public class XmodemRequest
    {
        /// <summary>
        /// 序号
        /// </summary>
        public int No { get; set; }
        /// <summary>
        /// 控制字符
        /// </summary>
        public byte ControlCharacter { get; set; }
        /// <summary>
        /// 包序号
        /// </summary>
        public byte PackageNo { get; set; }
        /// <summary>
        /// 包序号的反码
        /// </summary>
        public byte PackageInverseNo { get; set; }
        
        /// <summary>
        /// 校验位   这里只采用15的形式接收数据
        /// </summary>
        public byte[] CheckBit { get; set; }
        /// <summary>
        /// 数据
        /// </summary>

        private List<byte> dataList;
        public List<byte> DataList
        {
            get
            {
                if (null == dataList)
                {
                    dataList = new List<byte>();
                }
                return dataList;
            }

            set
            {
                dataList = value;
            }
        }

        
    }


   
}
